package net.sf.xmlform.springmvcexample.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.sf.xmlform.action.ActionException;
import net.sf.xmlform.data.ResultData;
import net.sf.xmlform.spring.annotation.Result;

@Controller
public class UserController {
	@Autowired
	private UserStore store;
	@RequestMapping(value="/user",method=RequestMethod.POST)
	@Result()
	public User addUser(User user){
		if(store.getUsers().containsKey(user.getCode()))
			throw new ActionException("c.user.exist","User exist");
		store.getUsers().put(user.getCode(),user);
		return user;
	}
	@RequestMapping(value="/user/{code}",method=RequestMethod.GET)
	@Result()
	public User user(@PathVariable("code") String code){
		return (User)store.getUsers().get(code);
	}
	@RequestMapping("/user/list")
	@Result()
	public ResultData users(){
		List list=new ArrayList(store.getUsers().values());
		ResultData result=new ResultData();
		result.setData(list);
		return result;
	}
	@RequestMapping("/user/anyDataWithMap1")
	@Result()
	public ResultData anyDataWithMap1(){
		List list=new ArrayList();
		Map map=new HashMap();
		map.put("code", "code");
		map.put("pwd", "pwd");
		list.add(map);
		ResultData result=new ResultData();
		result.setData(list);
		return result;
	}
	@RequestMapping("/user/anyDataWithMap2")
	@Result(Map.class)
	public List<Map> anyDataWithMap2(){
		List<Map> list=new ArrayList();
		Map map=new HashMap();
		map.put("code", "code");
		map.put("pwd", "pwd");
		list.add(map);
		return list;
	}
	
}
